/*#####################################*/
// Project Name : JSmartKit
// Author : Mr.Surachai Yindeeram
// Office : BrainStream Co.,Ltd.
// Create Date : Jan 3, 2013 3:37:20 PM
// Description : Session.java
/*#####################################*/
package com.xpaac.server;

import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class Session {
	
	public Session() {
		// TODO Auto-generated constructor stub
	}
	
	public static void setSession(String name,Object value){
		getHttpSession().setAttribute(name, value);
	}
	
	public static boolean removeSession(String name){
		if (getHttpSession().getAttribute(name.toLowerCase().trim()) != null) {
			getHttpSession().removeAttribute(name);
		}else{
//			Write to log or throw to exception
		}
		
		return true;
	}
	
	/**
	 * Get session from FaceContext
	 *@return {@link HttpSession}
	 */
	private static HttpSession getHttpSession() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpServletRequest httpServletRequest = (HttpServletRequest) facesContext.getExternalContext().getRequest();
		return httpServletRequest.getSession();
	}

	public static void clearSessionBean(String beanname) {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(beanname);
	}
	
	/**
	 * getAllSession on the server
	 *@return {@link Map}
	 */
	public static Map<String, Object> getAllSessoin(){
		return FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
	}
	
}
